﻿using CleverDD.Bll.Enum;
using CleverDD.Helper;
using HslCommunication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CleverDD.Bll
{
    public class CopyDemo
    {

        CommonTool _commonTool = new CommonTool();
        bool _threadFlag = true;



        #region 初始化常用Demo



        public void Init()
        {
            Task.Run(() =>
            {
                bool initPlcFlag = _commonTool.OpenAllPlc();
               // OperateResult result =  _commonTool.OpenPlc(PlcName.第一个Plc);
                StartThread();
            });
        }

        public void StartThread()
        {
            Thread readPlcThread = new Thread(ReadPlcAction);
            readPlcThread.Start();
        }


        /// <summary>
        /// 读取前会判断是否已连接
        /// </summary>
        public void ReadPlcAction()
        {
            while (_threadFlag)
            {
                Thread.Sleep(10);
                if ((int)PlcName.第二个Plc >= StaticTool.PlcToolList.Count)
                {
                    continue;
                }
                //多个Plc可以分开读取
                StaticTool.PlcToolList[(int)PlcName.第一个Plc].ReadAll();
                StaticTool.PlcToolList[(int)PlcName.第二个Plc].ReadAll();
            }
        }

        #endregion


        #region 读写常用


        public void ReadAndWrite()
        {
            _commonTool.GetPlcValue(PlcAddressName.Plc地址1);
            _commonTool.GetPlcValue(PlcAddressName.Plc地址1,PlcName.第二个Plc);

            _commonTool.WritePlcValue(PlcAddressName.Plc地址1, "0");
            _commonTool.WritePlcValue(PlcAddressName.Plc地址1, "1",PlcName.第二个Plc);

        }

       

        #endregion

    }
}
